<template>
  <ContentWrap>
    <!-- 搜索工作栏 -->
    <el-form
      class="-mb-15px"
      :model="queryParams"
      ref="queryFormRef"
      :inline="true"
      label-width="98px"
    >
      <!-- <el-form-item label="用户名称" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入用户名称"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item> -->
      <el-form-item label="用户真实姓名" prop="realName">
        <el-input
          v-model="queryParams.realName"
          placeholder="请输入用户真实姓名"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="用户账号" prop="mobile">
        <el-input
          v-model="queryParams.mobile"
          placeholder="请输入用户账号"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>

      <el-form-item label="账号启用状态" prop="status">
        <el-select
          v-model="queryParams.status"
          placeholder="请选择账号启用状态"
          clearable
          class="!w-240px"
        >
          <el-option
            v-for="dict in getIntDictOptions(DICT_TYPE.OPEN_STATUS)"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>

      <el-form-item label="认证状态" prop="reviewStatus">
        <el-select
          v-model="queryParams.reviewStatus"
          placeholder="请选择认证状态"
          clearable
          class="!w-240px"
        >
          <el-option
            v-for="dict in getIntDictOptions(DICT_TYPE.REVIEW_STATUS)"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button @click="handleQuery"><Icon icon="ep:search" class="mr-5px" /> 搜索</el-button>
        <el-button @click="resetQuery"><Icon icon="ep:refresh" class="mr-5px" /> 重置</el-button>
        <!-- <el-button
          type="primary"
          plain
          @click="openForm('create')"
          v-hasPermi="['jikong:user:create']"
        >
          <Icon icon="ep:plus" class="mr-5px" /> 新增
        </el-button> -->
        <el-button
          type="success"
          plain
          @click="handleExport"
          :loading="exportLoading"
          v-hasPermi="['jikong:user:export']"
        >
          <Icon icon="ep:download" class="mr-5px" /> 导出
        </el-button>
      </el-form-item>
    </el-form>
  </ContentWrap>

  <!-- 列表 -->
  <ContentWrap>
    <el-table v-loading="loading" :data="list" :stripe="true" :show-overflow-tooltip="true">
      <el-table-column label="用户ID" align="center" prop="id" />
      <el-table-column label="用户账号" align="center" prop="mobile" width="120" />
      <el-table-column label="用户真实姓名" align="center" prop="realName" width="120" />
      <el-table-column label="用户身份证号" align="center" prop="realNo" width="120" />
      <el-table-column label="地址信息" align="center" prop="userAddress" min-width="160" />
      <el-table-column label="账号启用状态" align="center" prop="status" width="120">
        <template #default="scope">
          <dict-tag :type="DICT_TYPE.OPEN_STATUS" :value="scope.row.status" />
        </template>
      </el-table-column>
      <el-table-column label="是否在黑名单" align="center" prop="blacklistStatus" width="120">
        <template #default="scope">
          <dict-tag
            :type="DICT_TYPE.BACKLIST_STATUS"
            :value="scope.row.blacklistStatus"
            width="120"
          />
        </template>
      </el-table-column>
      <el-table-column label="身份证人像面" align="center" prop="realPicPerson" width="120">
        <template #default="{ row }">
          <el-image
            :src="row.realPicPerson"
            class="h-30px w-30px"
            @click="imagePreview(row.realPicPerson)"
          />
        </template>
      </el-table-column>
      <el-table-column label="身份证国徽面" align="center" prop="realPicBack" width="120">
        <template #default="{ row }">
          <el-image
            :src="row.realPicBack"
            class="h-30px w-30px"
            @click="imagePreview(row.realPicBack)"
          />
        </template>
      </el-table-column>
      <el-table-column label="身份证手持" align="center" prop="realPicHolding" width="120">
        <template #default="{ row }">
          <el-image
            :src="row.realPicHolding"
            class="h-30px w-30px"
            @click="imagePreview(row.realPicHolding)"
          />
        </template>
      </el-table-column>
      <el-table-column label="认证状态" align="center" prop="reviewStatus" width="120">
        <template #default="scope">
          <dict-tag :type="DICT_TYPE.REVIEW_STATUS" :value="scope.row.reviewStatus" />
        </template>
      </el-table-column>
      <el-table-column
        label="认证通过时间"
        align="center"
        prop="passTime"
        :formatter="dateFormatter"
        width="180px"
      />
      <el-table-column
        label="注册时间"
        align="center"
        prop="createTime"
        :formatter="dateFormatter"
        width="180px"
      />

      <el-table-column label="操作" align="center" width="240" fixed="right">
        <template #default="scope">
          <el-button
            link
            type="primary"
            @click="openForm('update', scope.row.id)"
            v-hasPermi="['jikong:user:update']"
          >
            查看
          </el-button>
          <el-button
            link
            type="primary"
            :disabled="scope.row.blacklistStatus == 1"
            @click="updateBackListStatusHandler(scope.row.id, scope.row.blacklistStatus)"
            v-hasPermi="['jikong:user:update']"
          >
            加入黑名单
          </el-button>
          <el-button
            link
            type="primary"
            :disabled="scope.row.blacklistStatus == 0"
            @click="updateBackListStatusHandler(scope.row.id, scope.row.blacklistStatus)"
            v-hasPermi="['jikong:user:update']"
          >
            取消黑名单
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页 -->
    <Pagination
      :total="total"
      v-model:page="queryParams.pageNo"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />
  </ContentWrap>

  <!-- 表单弹窗：添加/修改 -->
  <UserForm ref="formRef" @success="getList" />
</template>

<script setup lang="ts">
import { getIntDictOptions, DICT_TYPE } from '@/utils/dict'
import { dateFormatter } from '@/utils/formatTime'
import download from '@/utils/download'
import { UserApi, UserVO } from '@/api/jikong/user'
import UserForm from './UserForm.vue'
import { createImageViewer } from '@/components/ImageViewer'

/** 用户 列表 */
defineOptions({ name: 'User' })

const message = useMessage() // 消息弹窗
const { t } = useI18n() // 国际化

const loading = ref(true) // 列表的加载中
const list = ref<UserVO[]>([]) // 列表的数据
const total = ref(0) // 列表的总页数
const queryParams = reactive({
  pageNo: 1,
  pageSize: 10,
  name: undefined,
  mobile: undefined,
  realName: undefined,
  status: undefined,
  blacklistStatus: undefined,
  reviewStatus: undefined
})
const queryFormRef = ref() // 搜索的表单
const exportLoading = ref(false) // 导出的加载中

/** 查询列表 */
const getList = async () => {
  loading.value = true
  try {
    const data = await UserApi.getUserPage(queryParams)
    list.value = data.list
    total.value = data.total
  } finally {
    loading.value = false
  }
}

/** 搜索按钮操作 */
const handleQuery = () => {
  queryParams.pageNo = 1
  getList()
}

/** 重置按钮操作 */
const resetQuery = () => {
  queryFormRef.value.resetFields()
  handleQuery()
}

/** 添加/修改操作 */
const formRef = ref()
const openForm = (type: string, id?: number) => {
  formRef.value.open(type, id)
}

/** 更新黑名单状态 */
const updateBackListStatusHandler = async (id: number, blacklistStatus: number) => {
  const blackStatus = blacklistStatus == 0 ? 1 : 0
  try {
    await UserApi.updateBackListStatus({ id, blacklistStatus: blackStatus })
    message.success(t('common.updateSuccess'))
    getList()
  } catch (error) {}
}

/** 导出按钮操作 */
const handleExport = async () => {
  try {
    // 导出的二次确认
    await message.exportConfirm()
    // 发起导出
    exportLoading.value = true
    const data = await UserApi.exportUser(queryParams)
    download.excel(data, '用户.xls')
  } catch {
  } finally {
    exportLoading.value = false
  }
}
/** 预览图片 */
const imagePreview = (imgUrl: string) => {
  createImageViewer({
    urlList: [imgUrl]
  })
}

/** 初始化 **/
onMounted(() => {
  getList()
})
</script>
